package com.gyf.base.common

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*

class AppManager private constructor() {
    private val activityStack: Stack<Activity> = Stack();

    companion object {
        val instance: AppManager by lazy { AppManager() }
    }

    fun addActivity(activity: Activity) {
        activityStack.add(activity)
    }

    fun finishActivity(activity: Activity) {
        activity.finish();
        activityStack.remove(activity);
    }

    fun finishActivityByName(cls: Class<*>) {
        for (activity in activityStack) {
            if (activity.javaClass.equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    fun currentActivity(activity: Activity): Activity {
        return activityStack.lastElement();
    }

    fun finishAllActivity() {
        for (activity in activityStack) {
            activity.finish();
        }
        activityStack.clear();
    }

    fun exitApp(context: Context) {
        finishAllActivity();
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager;
        activityManager.killBackgroundProcesses(context.packageName);
        System.exit(0);
    }
}